package com.hrhx.springboot.web;

import com.alibaba.fastjson.JSON;
import com.hrhx.duhongming.bean.EchartsConfig;
import com.hrhx.duhongming.core.EchartsCore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

@Controller
@RequestMapping("/echarts")
public class ThreadPoolDemo {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

//    private final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    private final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
    private final ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

    @RequestMapping("/threadPoolExecutor")
    @ResponseBody
    public String showFixedThreadPool(@RequestBody EchartsConfig echartsConfig){
//        ThreadPoolExecutor threadPoolExecutor= (ThreadPoolExecutor)singleThreadExecutor;
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("y0:taskCount",0);
        map.put("y0:completedTaskCount",0);
        map.put("y0:activeCount",0);
        map.put("y0:poolSize",0);
        map.put("y0:corePoolSize",0);
        map.put("y0:maximumPoolSize",0);
        map.put("x:时间",new Date());
        list.add(map);
        return EchartsCore.getInstance().adapterLineAndBar(echartsConfig,list).toString();
    }

    @Scheduled(cron="0/1 * *  * * ? ")
    public void getFixedThreadPool(){
        ThreadPoolExecutor threadPoolExecutor= (ThreadPoolExecutor)fixedThreadPool;
        Map<String,Object> map = new HashMap<>();
        map.put("taskCount",threadPoolExecutor.getTaskCount());
        map.put("completedTaskCount",threadPoolExecutor.getCompletedTaskCount());
        map.put("activeCount",threadPoolExecutor.getActiveCount());
        map.put("poolSize",threadPoolExecutor.getPoolSize());
        map.put("corePoolSize",threadPoolExecutor.getCorePoolSize());
        map.put("maximumPoolSize",threadPoolExecutor.getMaximumPoolSize());
        map.put("xxx",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        simpMessagingTemplate.convertAndSend("/topic/fixedThreadPool/data", JSON.toJSONString(map));
    }

    @Scheduled(cron="0/1 * *  * * ? ")
    public void getCachedThreadPool(){
        ThreadPoolExecutor threadPoolExecutor= (ThreadPoolExecutor)cachedThreadPool;
        Map<String,Object> map = new HashMap<>();
        map.put("taskCount",threadPoolExecutor.getTaskCount());
        map.put("completedTaskCount",threadPoolExecutor.getCompletedTaskCount());
        map.put("activeCount",threadPoolExecutor.getActiveCount());
        map.put("poolSize",threadPoolExecutor.getPoolSize());
        map.put("corePoolSize",threadPoolExecutor.getCorePoolSize());
        map.put("maximumPoolSize",threadPoolExecutor.getMaximumPoolSize());
        map.put("xxx",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        simpMessagingTemplate.convertAndSend("/topic/cachedThreadPool/data", JSON.toJSONString(map));
    }
}
